/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import br.ufmg.dees.insane.commons.dialogs.DecimalField;
import br.ufmg.dees.insane.commons.dialogs.SimpleDialog;
import br.ufmg.dees.insane.model.ModelState;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;
import br.ufmg.dees.insane.ui.rich.full.command.LastZoomCommand;

/**
 * A class representing creation dialog on screen.<br>
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.InsaneInternalInterface
 * @since July 2004
 */
public final class GridSettingsDialog extends SimpleDialog {

	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
    private JPanel gridPane = new JPanel();
    
    private DecimalField gridwidthField;
    private DecimalField gridheightField;
	
	private JTextField gridColorField = new JTextField();
    private JButton chooseGridColor = new JButton();
	
	private java.awt.geom.Rectangle2D.Double limits = new java.awt.geom.Rectangle2D.Double();
    private NumberFormat numberFormat;
    private ModelState state;
	
//*****************************************************************************
	
    /**
     * Constructor method, calling super with the especific title.
     *
     * @param parent The parent of the class.
     * @see gui.InsaneInternalInterface
     */
    public GridSettingsDialog(InsaneInternalInterface parent, ModelState s) {
    	this.parent = parent;
        state = s;
		
		chooseGridColor.setText(parent.getResourceBundle().getString("gChooseButton"));
		
        setUpFormat();
		
        //Sets fields according to model
        gridheightField  = new DecimalField(state.getYInc(), 10, numberFormat);
        gridwidthField  = new DecimalField(state.getXInc(), 10, numberFormat);
        
		// Sets up the grid panel
        JPanel gPane = new JPanel();
        gPane.setLayout(new BoxLayout(gPane, BoxLayout.Y_AXIS));
        gPane.add(gridPane);
        gridPane.setLayout(new GridLayout(4,2,2,2));
        gridPane.setMaximumSize(new Dimension(500,120));
        gridPane.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dGridDialog01")),
        BorderFactory.createEmptyBorder(2,2,2,2)));
        gridPane.add(new JLabel(parent.getResourceBundle().getString("dGridDialog02")));
        gridPane.add(new JLabel(parent.getResourceBundle().getString("dGridDialog03")));
        gridPane.add(gridwidthField);
        gridPane.add(gridheightField);
        gridPane.add(new JLabel(parent.getResourceBundle().getString("dGridDialog04")));
        gridPane.add(new JLabel());
        gridColorField.setEditable(false);
        gridColorField.setBackground(state.gridColor);
        gridPane.add(gridColorField);
        gridPane.add(chooseGridColor);
        chooseGridColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                gridColorActionPerformed(e);
            }
        } );
		
		setPane(gridPane);
		
    }
	
//*****************************************************************************
    
    public int showDialog() {
    	return super.showDialog(parent, parent.getResourceBundle().getString("dGridDialog01"));
    }
	
//*****************************************************************************
	
    /**
     * This method is executed on OK button click.
     *
     */
    public void approvedPerform() {
        try {
            state.setXYInc(gridwidthField.getValue(), gridheightField.getValue());
            state.gridColor = gridColorField.getBackground();
            
			state.setLimits(limits);
            
			parent.setModel(parent.getModel());
            this.setVisible(false);
			
			LastZoomCommand cmd = new LastZoomCommand(parent);
			cmd.execute();
			
        } catch (Exception err) {
            Toolkit.getDefaultToolkit().beep();
            this.setVisible(false);
        }
    }
    
//*****************************************************************************
	
    private void gridColorActionPerformed(ActionEvent e) {
    	gridColorField.setBackground(JColorChooser.showDialog(this, parent.getResourceBundle().getString("dGridDialog05"), gridColorField.getBackground()));
    }
    
//*****************************************************************************
	
    /**
     * Sets the number formater
     */
    private void setUpFormat() {
        numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
        numberFormat.setMaximumFractionDigits(3);
    }
	
//*****************************************************************************
}
